﻿#include "querypcinfo.h"
#include "ui_querypcinfo.h"





QueryPCInfo::QueryPCInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QueryPCInfo)
{
    ui->setupUi(this);

    timer = new QTimer(this);
    timer->start(1000);

    setWindowTitle(QStringLiteral("本机信息"));







    ui->txtPcName->setText(Global::getPcName()), ui->txtPcName->setEnabled(false);
    ui->txtLocal_IP->setText(Global::getLocal_IP()), ui->txtLocal_IP->setEnabled(false);
    ui->txtPcMemory->setText(getPcMemory()), ui->txtPcMemory->setEnabled(false);

    ui->txtAccountName->setText(Global::getAccountName()), ui->txtAccountName->setEnabled(false);

    ui->txtBatteryInfo->setText(QString::number(getBatteryInfo()) + "%"), ui->txtBatteryInfo->setEnabled(false);


    ui->progressBar->reset();

    ui->progressBar->setValue(getBatteryInfo());


    ui->txtDiskInfo->clear();


    QStringListIterator it(getDiskInfo());



    while(it.hasNext())
        ui->txtDiskInfo->addItem(it.next());








    ui->txtDiskInfo->setEnabled(false);





    connect(timer, SIGNAL(timeout()), this, SLOT(updateInfo_slot()));


}




const QString QueryPCInfo::getPcMemory() {
    MEMORYSTATUSEX statex;
    statex.dwLength = sizeof(statex);
    GlobalMemoryStatusEx(&statex);
    auto totalMem = statex.ullTotalPhys * 1.0 / Global::GB;
    auto freeMem = statex.ullAvailPhys * 1.0 / Global::GB;
    auto memDescribe = QString(QStringLiteral("可用 %1 GB / 共 %2 GB")).arg(QString::asprintf("%.2f", freeMem)).arg(QString::asprintf("%.2f", totalMem));

    return memDescribe;
}

const QStringList QueryPCInfo::getDiskInfo() {
    QStringList diskDescribe;
    QFileInfoList list = QDir::drives();
    foreach(QFileInfo dir, list) {
        QString dirName = dir.absolutePath();
        dirName.remove("/");
        LPCWSTR lpcwstrDriver = (LPCWSTR)dirName.utf16();
        ULARGE_INTEGER liFreeBytesAvailable, liTotalBytes, liTotalFreeBytes;
        if(GetDiskFreeSpaceEx(lpcwstrDriver, &liFreeBytesAvailable, &liTotalBytes, &liTotalFreeBytes)) {
            QString free = QString::number((double)liTotalFreeBytes.QuadPart / Global::GB, 'f', 1);
            free += "GB";
            QString all = QString::number((double)liTotalBytes.QuadPart / Global::GB, 'f', 1);
            all += "GB";
            QString str = QString(QStringLiteral("%1 可用 %2 / 共 %3")).arg(dirName, free, all);
            diskDescribe.append(str);




        }
    }

    return diskDescribe;
}

const int QueryPCInfo::getBatteryInfo() {
    SYSTEM_POWER_STATUS status;
    if(GetSystemPowerStatus(&status)) {
        if(status.BatteryFlag != 128)
            return status.BatteryLifePercent;

    }
    return -1;



}

void QueryPCInfo::updateInfo_slot() {
    ui->txtPcMemory->setText(getPcMemory());
    ui->txtBatteryInfo->setText(QString::number(getBatteryInfo()) + "%");



    ui->progressBar->setValue(getBatteryInfo());

    ui->txtDiskInfo->clear();
    QStringListIterator it(getDiskInfo());

    while(it.hasNext())
        ui->txtDiskInfo->addItem(it.next());

}


QueryPCInfo::~QueryPCInfo()
{
    delete ui;
}
